package app.server.project.cloud.random.db.entity;

import app.server.project.core.syndb.SynDBMgr;
import app.server.project.core.syndb.SynSpeed;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Transient;
import lombok.Data;
import org.hibernate.annotations.Comment;

import java.time.LocalDateTime;

@Data
@Entity(name = "dev_status")
@TableName("dev_status")
@Comment(value = "设备状态信息表")
@SynSpeed
public class DevStatusEntity {

    @Id
    @Column(name = "id")
    @Comment("用户主键")
    @TableId("id")
    private long id;

    @TableField("dev_id")
    @Comment("设备id")
    @Column(name = "dev_id")
    private long devId;

    @TableField("attar_id")
    @Comment("属性id")
    @Column(name = "attar_id")
    private String attarId;


    @TableField("attar_val")
    @Comment("属性值")
    @Column(name = "attar_val")
    private String attarVal;



    @TableField("create_time")
    @Comment("创建时间")
    @Column(name = "create_time")
    private LocalDateTime createTime;

    @Transient
    @TableField(exist = false)
    private boolean synDb = false;

    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(DevStatusEntity::getCreateTime, this);
    }


    public void setDevId(long devId) {
        this.devId = devId;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(DevStatusEntity::getDevId, this);
    }

    public void setAttarId(String attarId) {
        this.attarId = attarId;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(DevStatusEntity::getAttarId, this);
    }

    public void setAttarVal(String attarVal) {
        this.attarVal = attarVal;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(DevStatusEntity::getAttarVal, this);
    }


    public static DevStatusEntity of(long id, long devId,String attarId,String attarVal) {
        var ret=new DevStatusEntity();
        //打开同步代码标志
        ret.setSynDb(true);
        ret.setId(id);
        ret.setAttarVal(attarVal);
        ret.setAttarId(attarId);
        ret.setDevId(devId);
        ret.setCreateTime(LocalDateTime.now());
        return ret;
    }


}
